<template>
  <div>

    <el-container>
      <el-header>
        <Menu></Menu>
      </el-header>
      <el-container>
        <el-main>
          <div class="avt">
              <el-card class="box-card">
                <template #header>
                  <div class="card-header">
                    <span>
                      <div style="width: 100px">
                        <el-avatar src="src/assets/木户大圣笑.png" shape="square" size="large"/>
                      </div>
                    </span>
                  </div>
                </template>
                <div class="text item">昵称：{{nickName}}</div>
                <div class="text item">账号：{{uid}}</div>
                <div class="text item">个性签名：{{sign}}</div>
                <div>
                  <el-button size="large" style="float: left; padding: 2px" type="text" @click="dialog1Visible = true">
                    修改信息
                  </el-button>
                </div>
                <div>
                  <el-button size="large" style="float: left; padding: 2px" type="text" @click="dialog2Visible = true">
                    修改密码
                  </el-button>
                </div>
                <div style="float:right"> <el-button type="primary" plain @click="jump">管理用户</el-button></div><!--v-if="role=='admin'"-->
              </el-card>
            </div>

          <div class="post">
            <el-card class="box-card">
              <template #header>
                <div class="card-header">
                    <span>
                      <div class="text item">帖子</div>
                    </span>
                </div>
              </template>
              <div class="text item">累计发布：{{postNum}}</div>
            </el-card>
          </div>

          <div class="bottle">
            <el-card class="box-card">
              <template #header>
                <div class="card-header">
                    <span>
                      <div class="text item">漂流瓶</div>
                    </span>
                </div>
              </template>
              <div class="text item">累计扔出：{{ThrowBottleNum}}</div>
              <div class="text item">累计捞取：{{GetBottleNum}}</div>
            </el-card>
          </div>
        </el-main>
      </el-container>
      <el-dialog title="修改信息" v-model="dialog1Visible" width="40%">
        <el-form label-width="120px" size="small" :model="form" :rules="rules" ref="pass"
                 style="padding: 20px 80px 0 20px">
          <el-form-item label="头像" prop="nickName">
            <el-input v-model="avatarUrl"></el-input>
          </el-form-item>
          <el-form-item label="昵称" prop="nickName">
            <el-input v-model="nickName"></el-input>
          </el-form-item>
          <el-form-item label="个性签名" prop="sign">
            <el-input v-model="sign"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submit">确 定</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
      <el-dialog title="修改密码" v-model="dialog2Visible" width="40%">
        <el-form label-width="120px" size="small" :model="form" :rules="rules" ref="pass"
                 style="padding: 20px 80px 0 20px">
          <el-form-item label="原密码" prop="password">
            <el-input v-model="form.password" autocomplete="off" show-password></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newPassword">
            <el-input v-model="form.newPassword" autocomplete="off" show-password></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="confirmPassword">
            <el-input v-model="form.confirmPassword" autocomplete="off" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="save">确 定</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-container>
  </div>
</template>

<script>
//import { userInfo, updateUser } from "@/api/user.js";
import Menu from '../components/Menu.vue'
export default {
  name: "UserInfo",
  components: {Menu},
  data(){
    return {
      dialog1Visible: false,
      dialog2Visible: false,
      form: {},  // username, password, newPassword, confirmNewPassword 这4个属性
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      rules: {
        nickName:[
          {required: false, rigger: 'blur'},
        ],
        sign:[
          {required: false, rigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入原密码', trigger: 'blur'},
          {min: 3, message: '长度不少于5位', trigger: 'blur'}
        ],
        newPassword: [
          {required: true, message: '请输入新密码', trigger: 'blur'},
          {min: 5, message: '长度不少于5位', trigger: 'blur'}
        ],
        confirmPassword: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 5, message: '长度不少于5位', trigger: 'blur'}
        ]
      },
      avatarUrl:"src/assets/木户大圣笑.png",//头像
      nickName:"参商",//昵称
      uid:"19560737796",//账号
      sign:"宝矿力",//个性签名
      postNum:"0",
      likeNum:"0",
      ThrowBottleNum:"0",
      GetBottleNum:"0",
    };
  },
  mounted() {
    //this.load();暂时不挂载
  },
  methods:{
    jump() {
      this.$router.push('/manageuser')
    }
  }
  /*methods:{
    load() {
      userInfo(this.$route.params.id)
          .then((res) => {
            this.avatarUrl = res.data.avatarUrl;
            this.nickName = res.data.nickName;
            this.uid = res.data.uid;
            this.sign = res.data.sign;
            this.postNum = res.data.postNum;
            this.likeNum = res.data.likeNum;
            this.ThrowBottleNum = res.data.ThrowBottleNum;
            this.GetBottleNum = res.data.GetBottleNum;
          })
          .catch((err) => {
            console.log(err);
          });
    },
    submit() {
      updateUser(this.form)
        .then((res) => {
          console.log(res);
          this.dialogVisible = false;
          this.$emit("flesh");
        })
        .catch((err) => {
          console.log(err);
        });
    },
    save() {
      this.$refs.pass.validate((valid) => {
        if (valid) {  //合法
          if (this.form.newPassword !== this.form.confirmPassword) {
            this.$message.error("2次输入的新密码不相同")
            return false
          }
          this.form.username = this.user.username
          this.rq.post("/user/ch_passwd", this.form).then(res => {
            if (res.code === '200') {
              this.$message.success("修改成功")
              window.localStorage.removeItem('user')
              this.$router.push({path: '/login'})
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }*/
}
</script>

<style scoped>
.avt {
  position: fixed;
  top: 130px;
  left: 9%;
  width: 20px;
}
.post{
  position: fixed;
  top: 130px;
  left: 39%;
  width: 20px;
}
.bottle{
  position: fixed;
  top: 130px;
  left: 69%;
  width: 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.text {
  font-size: 18px;
  text-align: left;
}
.item {
  margin-bottom: 18px;
}
.box-card {
  width: 360px;
  height: 500px;
}
</style>
